<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 5.2.4: Solves a simple QCQP</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch05_duality/html/qcqp.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 5.2.4: Solves a simple QCQP</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 08/23/05</span>
<span class="comment">%</span>
<span class="comment">% Solved a QCQP with 3 inequalities:</span>
<span class="comment">%           minimize    1/2 x'*P0*x + q0'*r + r0</span>
<span class="comment">%               s.t.    1/2 x'*Pi*x + qi'*r + ri &lt;= 0   for i=1,2,3</span>
<span class="comment">% and verifies that strong duality holds.</span>

<span class="comment">% Input data</span>
randn(<span class="string">'state'</span>,13);
n = 6;
P0 = randn(n); P0 = P0'*P0 + eps*eye(n);
P1 = randn(n); P1 = P1'*P1;
P2 = randn(n); P2 = P2'*P2;
P3 = randn(n); P3 = P3'*P3;
q0 = randn(n,1); q1 = randn(n,1); q2 = randn(n,1); q3 = randn(n,1);
r0 = randn(1); r1 = randn(1); r2 = randn(1); r3 = randn(1);

fprintf(1,<span class="string">'Computing the optimal value of the QCQP and its dual... '</span>);

cvx_begin
    variable <span class="string">x(n)</span>
    dual <span class="string">variables</span> <span class="string">lam1</span> <span class="string">lam2</span> <span class="string">lam3</span>
    minimize( 0.5*quad_form(x,P0) + q0'*x + r0 )
    lam1: 0.5*quad_form(x,P1) + q1'*x + r1 &lt;= 0;
    lam2: 0.5*quad_form(x,P2) + q2'*x + r2 &lt;= 0;
    lam3: 0.5*quad_form(x,P3) + q3'*x + r3 &lt;= 0;
cvx_end

obj1 = cvx_optval;
P_lam = P0 + lam1*P1 + lam2*P2 + lam3*P3;
q_lam = q0 + lam1*q1 + lam2*q2 + lam3*q3;
r_lam = r0 + lam1*r1 + lam2*r2 + lam3*r3;
obj2 = -0.5*q_lam'*inv(P_lam)*q_lam + r_lam;

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The duality gap is equal to '</span>);
disp(obj1-obj2)
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal value of the QCQP and its dual...  
Calling SDPT3: 35 variables, 10 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 10
 dim. of socp   var  = 32,   num. of socp blk  =  4
 dim. of linear var  =  3
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|2.0e+01|2.1e+00|5.5e+02| 1.197168e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.687|1.3e-06|6.8e-01|9.6e+01| 8.394112e+00 -3.713950e+00| 0:0:00| chol  1  1 
 2|1.000|0.971|2.9e-06|2.3e-02|6.6e+00| 9.243206e-01 -3.202859e+00| 0:0:00| chol  1  1 
 3|0.836|1.000|1.4e-06|3.6e-04|1.0e+00|-2.412332e+00 -3.408622e+00| 0:0:00| chol  1  1 
 4|0.840|0.832|3.9e-07|9.2e-05|2.0e-01|-3.094383e+00 -3.291935e+00| 0:0:00| chol  1  1 
 5|0.927|1.000|9.6e-08|3.7e-06|3.0e-02|-3.204453e+00 -3.233858e+00| 0:0:00| chol  1  1 
 6|0.945|0.935|1.5e-08|6.0e-07|1.8e-03|-3.224662e+00 -3.226481e+00| 0:0:00| chol  1  1 
 7|0.934|0.905|9.5e-09|9.3e-08|1.4e-04|-3.225852e+00 -3.225989e+00| 0:0:00| chol  1  1 
 8|0.959|0.915|1.7e-09|9.8e-09|9.7e-06|-3.225934e+00 -3.225943e+00| 0:0:00| chol  1  1 
 9|0.990|0.988|9.7e-10|4.7e-10|4.4e-07|-3.225938e+00 -3.225939e+00| 0:0:00| chol  1  1 
10|0.972|0.956|2.1e-10|6.8e-11|2.1e-08|-3.225938e+00 -3.225938e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -3.22593846e+00
 dual   objective value = -3.22593847e+00
 gap := trace(XZ)       = 2.14e-08
 relative gap           = 2.87e-09
 actual relative gap    = 2.30e-09
 rel. primal infeas     = 2.12e-10
 rel. dual   infeas     = 6.83e-11
 norm(X), norm(y), norm(Z) = 1.0e+01, 1.5e+00, 1.9e+00
 norm(A), norm(b), norm(C) = 1.5e+01, 7.9e+00, 7.3e+00
 Total CPU time (secs)  = 0.12  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 2.2e-10  0.0e+00  8.3e-11  0.0e+00  2.3e-09  2.9e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.895296
 
Done! 
------------------------------------------------------------------------
The duality gap is equal to 
   1.2878e-08

</pre>
</div>
</body>
</html>